<?php 

Class shakeModel extends appModel
{
	//构建微信用户数组
	public function buildArr($arr)
	{
		$city= $arr['city'];//普通用户个人资料填写的城市
		
		$country= $arr['country'];//国家，如中国为CN
			
		$province = $arr['province'];//用户个人资料填写的省份
		
		if($country == "中国台湾")
		{
			$province = "台湾";
		}
		elseif($country == "中国香港")
		{
			$province = "香港";
		}
		elseif($country == "中国澳门")
		{
			$province = "澳门";
		}
		
		$arr['city_code']=$this->get_city($city,$province);
			
		$arr['province_code']=$this->get_province($province);
			
		$arr['createtime']=$_SERVER['REQUEST_TIME'];
		
		return $arr;
	}
	
	//获得分数级别
	public function get_score($step,$second)
	{
		if($second >20)
		{
			$times = 10;
		}
		else if($second >10)
		{
			$times = 20;
		}
		else
		{
			$times = 30;
		}
		
		$score = $times*$step;
		
		
		return $score;
	}
	
	
	//获得省份名称
	public function get_province($province)
	{
		if($province=="")
			return "";
		
		$sql="SELECT id FROM ".$this->table("areas")." WHERE name like '%".$province."%'  and pid = 0 ";
		
		$id=$this->getOne($sql);
		
		return $id;
	}
	
	//获得城市名称
	public function get_city($city,$province)
	{
		if($city=="")
			return "";
		
		$sql="SELECT id FROM ".$this->table("areas")." WHERE name like '%".$city."%'  and pid > 0 ";
		
		$id=$this->getOne($sql);
		
		if(!$id)
		{
			$sql="SELECT id FROM ".$this->table("areas")." WHERE name like '%".$province."%'  and pid >  0 ";
			
			$id=$this->getOne($sql);
		}
		
		return $id;
		
	}
	


	//获得一条微信record记录
	public function get_record($table,$id)
	{
		
		if(!$id)
		{
			return false;
		}
		
		$sql="SELECT p1.*,p2.headimgurl as img , p2.nickname as name , p1.name as pname FROM ".$this->table($table)." as p1 left join ".$this->table('wxusers')." as p2 on  p1.wxid=p2.id WHERE  p1.wxid = ".$id;
			
// 		echo $sql;
		$result=$this->getRow($sql);
	
		if($result)
		{
	
			return $result;
		}
		else
		{
			return false;
		}
	}
	
	
	//获得一组微信record记录
	public function get_records($table,$limit)
	{
	
		
		$sql="SELECT (p1.score + p1.add_score)  as total , p1.*,p2.headimgurl as img , p2.nickname as name , p1.name as pname FROM ".$this->table($table)." as p1 left join ".$this->table('wxusers')." as p2 on  p1.wxid=p2.id order by total desc limit ".$limit;
			
		// 		echo $sql;
		$result=$this->getAll($sql);
	
		if($result)
		{
	
			return $result;
		}
		else
		{
			return false;
		}
	}
	
	
	//清除record记录
	public function clear($table)
	{
		
			
			
			$sql="DELETE FROM ".$this->table($table)." WHERE 1=1 ";
			
		
			return $res=$this->query($sql);
		
	}
	
	
	//自增
	public function setInc($table,$field,$where,$foot)
	{
		if($where)
		{
				
			$sql="UPDATE ".$this->table($table)." SET ".$field."=".$field."+ ".$foot." ".$where;
				
			
			return $res=$this->query($sql);
		}
		
		return false;
	}
	
	
	//更新一条record记录
	public function update_record($table,$info,$id)
	{
		
		if($id)
		{
			foreach($info as $k=>$v)
			{
				if($v!='' || $v==0)
				{
					$where.=" $k='$v',";
	
				}
			}
			$where.=" addtime=addtime ";
			
			$sql="UPDATE ".$this->table($table)." SET ".$where." WHERE wxid='".$id."'";
			//echo $sql;
			return $res=$this->query($sql);
		}
	}
	
	//更新一条助力join记录
	public function update_join($table,$info,$wxid=0)
	{
	
		if($wxid)
		{
			foreach($info as $k=>$v)
			{
				if($v!='' || $v==0)
				{
					$where.=" $k='$v',";
	
				}
			}
			$where.=" addtime=addtime ";
				
			$sql="UPDATE ".$this->table($table)." SET ".$where." WHERE wxid='".$wxid."'  limit 1 ";
			//echo $sql;
			return $res=$this->query($sql);
		}
	}
	
	
	//插入一条record记录
	public function create_record($table,$info)
	{
		
		if($info['wxid']>0)
		{
			
			//判断是否已存在
			$back = self::get_record($table, $info['wxid']);
			if($back)
			{
				
				return false;
			}
			
		}
		else 
		{
			return false;
		}
		
		$field='';
		$value='';
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
					
					
				$value.="'$v',";
	
			}
		}
		
		$field.=" addtime ";
		$value.=time();
	
		$sql="INSERT INTO ".$this->table($table)." (".$field.") VALUES(".$value.")";
		
// 		$sql2="update ".$this->table("shake_total")." set joinnums = joinnums +1 ";
		
// 		$this->query($sql2);
		
		return $res=$this->query($sql);
	
	
	
	}
	
	
	//抽奖操作
	public function get_prize($table,$wxid)
	{
	
		$sql="SELECT * FROM ".$this->table($table)." as p1  limit 1 ";
			
		$result=$this->getRow($sql);
	
		$joinnums = $result['joinnums']+1;
	
	
		if(floor($joinnums/10)>$result['prize1'] &&$result['prize1'] < 500 )
		{
			$prize = 1;
			
				
		}
		else
		{
			$prize = -1;
		
		}
	

	
		$field = "prize".$prize;
	
		if($prize>0)
		{
			$str =" prizenums = prizenums+1 , joinnums = joinnums+1 , ".$field." = ".$field." +1 ";
		}
		else
		{
			$str ="  joinnums = joinnums+1 ";
		}
	
		$sql="UPDATE ".$this->table($table)."  SET ".$str."  WHERE 1=1";
		// 		self::setInc('shake_total',$field," where  1=1 ",1);
		// 		$sql="UPDATE ".$this->table("shake_total")." SET  joinnums = joinnums+1  WHERE 1=1";
	
		$this->query($sql);
	
	
		
		$info['prize'] = $prize;
	
		self::update_shake_record($table, $info, $wxid);
	
	
		return $prize;
		
	
	}
	
	
	//插入一条助力join记录
	public function create_join($table,$info,$where)
	{
		
		if($info['wxid']>0&&$info['pid']>0)
		{
			$sql = " select count(*) from ".$this->table($table).$where;
			//$where = " wxid = ".$info['wxid'];
			
			$back = $this->getOne($sql);
			
			//$back = self::get_join($table, $where);
			
			if($back)
			{
				
				return false;
			}
	
		}
		
		
		// 		else
			// 		{
			// 			return false;
			// 		}
	
		$field='';
		$value='';
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
					
					
				$value.="'$v',";
	
			}
		}
	
		$field.=" addtime ";
		$value.=time();
	
		$sql="INSERT INTO ".$this->table($table)." (".$field.") VALUES(".$value.")";
	
	
		return $res=$this->query($sql);
	
	
	
	}
	
	//获取一位微信用户的所有助力join记录
	public function get_joins($table,$id)
	{
		$sql="SELECT p1.*,p2.headimgurl as img , p2.nickname as name FROM ".$this->table($table)." as p1 left join ".$this->table('wxusers')." as p2 on  p1.wxid=p2.id WHERE  p1.pid = ".$id." order by addtime desc ";
			
	
		$result=$this->getAll($sql);
	
		if($result)
		{
			return $result;
		}
		else
		{
			return array();
		}
	}
	
	
	//获取一条助力join记录
	public function get_join($table,$id)
	{
		$sql="SELECT p1.*  FROM ".$this->table($table)." as p1  WHERE  p1.wxid = ".$id;
			
		$result=$this->getRow($sql);
	
		if($result)
		{
			return $result;
		}
		else
		{
			return array();
		}
	}
	
}